package com.cycleit.applicationexample.business {
	
	import com.cycleit.applicationexample.model.ExampleResponseModel;
	import com.cycleit.applicationexample.responder.FacadeResponder;
	import com.cycleit.applicationexample.vo.user.User;
	
	import mx.collections.ArrayCollection;
	import mx.rpc.IResponder;
	
	// ExampleServiceFacade implements singlenton pattern. We use a private SingleClass to pseudo-implement this pattern, cause
	// ActionScript3 does not allow private constructors.
	public class ExampleServiceFacade {

		private static var _instance:ExampleServiceFacade;
		
		public static function getInstance():ExampleServiceFacade{
        	if(_instance == null) {
	       		_instance = new ExampleServiceFacade(new SingleClass());
	    	}
	    	return _instance;
        }
        
		public function ExampleServiceFacade(v:SingleClass) {
		}
		
		/**
		 * Business logic.
		 * 
		 */ 

		/***********
		 *  LOGIN  *
		 ***********/
		public function login(login:String, password:String, rp:IResponder = null):void{
			var user:User = new User();
			user.login = login;
			user.password = password;
			user.name = "Javier";
			user.secondName = "Molina Castillo";
			
			ExampleResponseModel.getInstance().loginData = user;
		} 
		
		/****************
		 *  ENUMERATES  *
		 ****************/
		public function getCountries(rp:IResponder = null):void{
			ExampleResponseModel.getInstance().getCountriesData = new ArrayCollection([{description:"Germany"},{description:"France"},{description:"Spain"},{description:"Italy"}]);
		} 
		public function getOccupations(rp:IResponder = null):void{
			ExampleResponseModel.getInstance().getOccupationsData = new ArrayCollection([{description:"Student"},{description:"Gov. Employee"},{description:"Freelance"},{description:"Entrepreneur"}]);
		} 
		
		/***********
		 *  USERS  *
		 ***********/
		public function getUsers(rp:IResponder = null):void{
			var user1:User = new User();
			user1.name = "Javier";
			user1.secondName = "Molina Castillo";
			user1.nationality = "Spain";
			user1.occupation = "Entreprenuer";
			
			var user2:User = new User();
			user2.name = "Ramón";
			user2.secondName = "García";
			user2.nationality = "Spain";
			user2.occupation = "Entrepreneur";
			
			var user3:User = new User();
			user3.name = "Carlo";
			user3.secondName = "Turturro";
			user3.nationality = "Italy";
			user3.occupation = "Student";
			
			var users:ArrayCollection = new ArrayCollection([user1, user2, user3]);
			ExampleResponseModel.getInstance().getUsersData = users;
		} 
		public function getUserById(id:Number, rp:IResponder = null):void{
			var user:User = new User();
			user.name = "Carlo";
			user.birthday = new Date();
			user.secondName = "Turturro";
			user.nationality = "Italy";
			user.occupation = "Student";
			user.studies = "Software engineer at Politechnic University of Madrid.";
			user.cv = "Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem.";
			ExampleResponseModel.getInstance().getUserByIdData = user;
		} 
		
		//metodos privados
		private function getResponder(rp:IResponder):IResponder{
			if(!rp){
				rp = new FacadeResponder(ExampleResponseModel.getInstance());
			}
			return rp;
		}
	}
}
class SingleClass {}